package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.util.SevenUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/images")
public class ImageController {
    @Autowired
    private ServletContext ctx;
    @RequestMapping("/upload")
    public Object upload(MultipartFile upload){
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            //每次上传图片的位置是指定的位置
            String url = SevenUploadUtil.uploadQiniu(upload);
            //返回图像的url
            map.put("url",url);
            //表示上传成功
            map.put("uploaded",1);
        }catch (Exception e){
            //上传失败 status = 0
            map.put("uploaded",0);
            map.put("msg",e.getMessage());
            Map<String,Object> error = new HashMap<String, Object>();
            error.put("message",e.getMessage());
            map.put("error",error);
        }
        return map;
    }
}
